starlark: add maxAlloc guard to list, sorted, enumerate#636
starlark: add maxAlloc guard to list, sorted, enumerate#636YuvalFradkin1 wants to merge 1 commit intogoogle:masterfrom
Conversation
Prevents panic: runtime error: makeslice: cap out of range when list/sorted/enumerate receive a large range() argument. Applies the same maxAlloc=1<<30 guard already used in *Repeat helpers.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Thanks. The code changes here look fine to me as a means to prevent accidents, but I fear this is at best a band-aid covering the most trivial ways in which it is possible to cause the interpreter to allocate too much memory. A creative adversary can easily find more complicated ways to do it, and I think it's unlikely that there will be a systematic way to avoid such problems by handling Go OOM conditions. (Even if all allocations performed by the interpreter used a different allocator, application-defined built-in functions would not.) That said, the iterable returned by |
list(), sorted(), and enumerate() preallocate a backing slice using
Len(iterable) as the capacity with no upper-bound check. When called
with a large range() value (e.g. list(range(4611686018427387904))),
the resulting make([]Value, 0, 2^62) overflows and the Go runtime panics:
panic: runtime error: makeslice: cap out of range
go.starlark.net/starlark.list(...)
starlark/library.go:689
The panic is unrecovered and kills the host process. Any application
evaluating untrusted Starlark is affected.
The *Repeat helpers in eval.go already define and enforce maxAlloc=1<<30
for string/bytes/tuple repetition. This PR applies the same guard to
list(), sorted(), and enumerate().
Fixes: https://issuetracker.google.com/issues/499639516